package com.dd.shop4j.console.member.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.dd.shop4j.common.utils.Constant;
import com.dd.shop4j.common.utils.ExcelUtils;
import com.dd.shop4j.common.utils.Message;
import com.dd.shop4j.common.utils.Message.MessageType;
import com.dd.shop4j.console.member.controller.forms.MemberForm;
import com.dd.shop4j.console.member.service.MemberManagerService;
import com.dd.shop4j.member.model.MemberAddress;
import com.dd.shop4j.member.model.MemberInfo;
import com.dd.shop4j.sys.service.SysConstantService;

@Controller
@RequestMapping("/admin/member")
public class MemberListController {
	
	@Resource
	public MemberManagerService memberManagerService;
	
	@Resource
	private SysConstantService sysConstantService;
	
	@RequestMapping("/list")
	public ModelAndView init(MemberInfo memberInfo, Integer curpage, Integer pagesize) {
		if (curpage == null || curpage <= 0) {
			curpage = 1;
		}
		if (pagesize == null || pagesize <= 0) {
			pagesize = sysConstantService.getInteger(Constant.PAGE_SIZE_CODE);
		}
		ModelAndView view = new ModelAndView("/admin/member/member_list");
		view.addObject("query", memberInfo);
		view.addObject("page", memberManagerService.queryMemberInfoByPage(memberInfo, curpage, pagesize));
		return view;
	}
	
	@RequestMapping("/export")
	public void export(HttpServletResponse response) throws Exception {
		String codedFileName = java.net.URLEncoder.encode("会员导出列表", "UTF-8");  
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
        List<MemberInfo> memberInfos = memberManagerService.queryMemberInfos(null);
        List<List<Object>> data = ExcelUtils.getExcelData(memberInfos);
		ExcelUtils.writeExcel(response.getOutputStream(), data);
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.GET)
	public ModelAndView insert() {
		ModelAndView view = new ModelAndView("/admin/member/member_insert");
		return view;
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ModelAndView insert(MemberForm memberForm) {
		memberManagerService.insertMember(memberForm);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setGotoPage("/admin/member/insert");
		message.setContext("恭喜，会员信息保存成功。");
		return message.getView();
	}
	
	@RequestMapping(value = "/delete/{memberId}", method = RequestMethod.GET)
	public ModelAndView deleteMember(@PathVariable("memberId") Integer memberId) {
		memberManagerService.deleteMember(memberId);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setContext("恭喜，删除会员成功。");
		message.setGotoPage("/admin/member/list");
		return message.getView();
	}
	
	
	@RequestMapping(value = "/update/{memberId}", method = RequestMethod.GET)
	public ModelAndView update(@PathVariable("memberId") Integer memberId) {
		ModelAndView view = new ModelAndView("/admin/member/member_update");
		MemberInfo member = memberManagerService.loadMemberById(memberId);
		view.addObject("member", member);
		return view;
	}
	
	@RequestMapping(value = "/update/{memberId}", method = RequestMethod.POST)
	public ModelAndView update(MemberForm form) {
		memberManagerService.updateMember(form);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setContext("恭喜，会员更新成功。");
		message.setGotoPage("/admin/member/update/" + form.getMemberInfo().getId());
		return message.getView();
	}
	
	@RequestMapping(value = "/addressManager/{memberId}", method = RequestMethod.GET)
	public ModelAndView updateAddress(@PathVariable("memberId") Integer memberId) {
		MemberAddress address = new MemberAddress();
		address.setMemberId(memberId);
		List<MemberAddress> memberAddress = memberManagerService.queryMemberAddress(address);
		ModelAndView view = new ModelAndView("/admin/member/member_address");
		view.addObject("memberAddress",memberAddress);
		view.addObject("memberId",memberId);
		return view;
	}
	
	@RequestMapping(value = "/addressManager/insert", method = RequestMethod.POST)
	public ModelAndView insertMemberAddress(MemberForm memberForm) {
		memberManagerService.insertAddress(memberForm);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setContext("恭喜，收货地址新增成功。");
		message.setGotoPage("/admin/member/addressManager/"+ memberForm.getMemberAddress().getMemberId());
		return message.getView();
	}
	
	@RequestMapping(value = "/addressManager/delete/{addressId}&{memberId}", method = RequestMethod.GET)
	public ModelAndView deleteMemberAddress(@PathVariable("addressId") Integer addressId,@PathVariable("memberId") Integer memberId) {
		memberManagerService.deleteMemberAddress(addressId);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setContext("恭喜，删除地址成功。");
		message.setGotoPage("/admin/member/addressManager/"+ memberId);
		return message.getView();
	}
	
	@RequestMapping(value = "/addressManager/update/{addressId}", method = RequestMethod.POST)
	public ModelAndView updateMemberAddress(MemberForm form) {
		memberManagerService.updateMemberAddress(form);
		Message message = new Message(MessageType.CONSOLE_SUCCESS);
		message.setContext("恭喜，会员地址更新成功。");
		message.setGotoPage("/admin/member/addressManager/" + form.getMemberAddress().getMemberId());
		return message.getView();
	}
	
	@RequestMapping(value = "/addressManager/update/{addressId}", method = RequestMethod.GET)
	public ModelAndView updateMemberAddress(@PathVariable("addressId") Integer addressId) {
		ModelAndView view = new ModelAndView("/admin/member/member_insert");
		MemberAddress memberAddress = memberManagerService.loadMemberAddressById(addressId);
		view.addObject("memberAddress", memberAddress);
		return view;
	}
}